﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
namespace ERP.Classes
{
    public class cs_erp_khachhang_sodoInfo
    {
        private int _id_thuoctinh;
        private int _id_hh;
        private int _id_khachhang;
        private string _giatri;
        private DateTime _ngaynhap;
        private string _ghichu;

        public cs_erp_khachhang_sodoInfo()
        {
            this._id_thuoctinh = 0;
            this._id_hh = 0;
            this._id_khachhang = 0;
            this._giatri = " ";
            this._ngaynhap = Convert.ToDateTime("01/01/9999");
            this._ghichu = " ";

        }
        public int id_thuoctinh
        {
            get
            {
                return this._id_thuoctinh;
            }
            set
            {
                this._id_thuoctinh = value;
            }
        }
        public int id_hh
        {
            get
            {
                return this._id_hh;
            }
            set
            {
                this._id_hh = value;
            }
        }
        public int id_khachhang
        {
            get
            {
                return this._id_khachhang;
            }
            set
            {
                this._id_khachhang = value;
            }
        }
        public string giatri
        {
            get
            {
                return this._giatri;
            }
            set
            {
                this._giatri = value;
            }
        }
        public DateTime ngaynhap
        {
            get
            {
                return this._ngaynhap;
            }
            set
            {
                this._ngaynhap = value;
            }
        }
        public string ghichu
        {
            get
            {
                return this._ghichu;
            }
            set
            {
                this._ghichu = value;
            }
        }

        public int ExcuteStoreProcedure(string storeProcedure, int option)
        {
            string[] paraName = new string[7];
            paraName[0] = "@id_thuoctinh";
            paraName[1] = "@id_hh";
            paraName[2] = "@id_khachhang";
            paraName[3] = "@giatri";
            paraName[4] = "@ngaynhap";
            paraName[5] = "@ghichu";
            paraName[6] = "@option";
            object[] paraValue = new object[7];
            paraValue[0] = this._id_thuoctinh;
            paraValue[1] = this._id_hh;
            paraValue[2] = this._id_khachhang;
            paraValue[3] = this._giatri;
            paraValue[4] = this._ngaynhap;
            paraValue[5] = this._ghichu;
            paraValue[6] = option;
            csCSDL csdl = new csCSDL();
            int i = 0;
            i = csdl.CreateParameters(paraName, 7, paraValue);
            return csdl.ExcuteStoreProcedure(storeProcedure);
        }
        public int ExcuteProcedure(int option)
        {
            return this.ExcuteStoreProcedure("Proc_ERP_KHACHHANG_SODO", option);
        }

        public void ShowDataInCombo(ComboBox cbo, string condition)
        {
            csComboBox cb = new csComboBox("id_hh", "id_thuoctinh");
            cb.KetDuLieu(cbo, "ERP_KHACHHANG_SODO", condition);
        }

        public int ShowDataGrid(DataGrid datagrid, string condition)
        {
            csDataGrid grid = new csDataGrid();

            string[] header = new string[6];
            header[0] = "id_thuoctinh";
            header[1] = "id_hh";
            header[2] = "id_khachhang";
            header[3] = "giatri";
            header[4] = "ngaynhap";
            header[5] = "ghichu";

            string[] mappingname = new string[6];
            mappingname[0] = "id_thuoctinh";
            mappingname[1] = "id_hh";
            mappingname[2] = "id_khachhang";
            mappingname[3] = "giatri";
            mappingname[4] = "ngaynhap";
            mappingname[5] = "ghichu";

            int[] colwidth = new int[6];
            colwidth[0] = 12;
            colwidth[1] = 12;
            colwidth[2] = 12;
            colwidth[3] = 1500;
            colwidth[4] = 24;
            colwidth[5] = 1500;
            csCSDL csdl = new csCSDL();
            return grid.CreateTableStyle("ERP_KHACHHANG_SODO", datagrid, csdl.OpenTable("ERP_KHACHHANG_SODO", condition), header, mappingname, colwidth, 6);
        }
        public int Get_Info()
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                ds = csdl.OpenTable("ERP_KHACHHANG_SODO", "");
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    return 0;
                }
                this._id_thuoctinh = Convert.ToInt32(ds.Tables[0].Rows[0]["id_thuoctinh"].ToString().Trim());
                this._id_hh = Convert.ToInt32(ds.Tables[0].Rows[0]["id_hh"].ToString().Trim());
                this._id_khachhang = Convert.ToInt32(ds.Tables[0].Rows[0]["id_khachhang"].ToString().Trim());
                this._giatri = ds.Tables[0].Rows[0]["giatri"].ToString().Trim();
                this._ngaynhap = Convert.ToDateTime(ds.Tables[0].Rows[0]["ngaynhap"].ToString().Trim());
                this._ghichu = ds.Tables[0].Rows[0]["ghichu"].ToString().Trim();
                return 1;
            }
            catch
            {
                return -1;
            }
        }
        public DataSet Get_List()
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                string sql = "select a.id_thuoctinh, a.id_hh, d.tenhh, c.tenthuoctinh, b.giatri, b.ngaynhap, b.ghichu "
                + " from ERP_HH_TT a"
                + " left join ERP_HANGHOA d on (d.id_hh = a.id_hh)"
                + " left join ERP_KHACHHANG_SODO b on (a.id_thuoctinh = b.id_thuoctinh and a.id_hh = b.id_hh and b.id_khachhang = " + this._id_khachhang + ")"
                + " left join ERP_HH_THUOCTINH c on (c.id_thuoctinh = a.id_thuoctinh)"
                + " where a.sudung = 1";
                ds = csdl.SelecSQL(sql);

                return ds;
            }
            catch
            {
                return null;
            }
        }

        public DataSet Get_List_Nhapsodo_Theo_HD(int hd)
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                string sql = "select a.id_thuoctinh, a.id_hh, d.tenhh, c.tenthuoctinh, b.giatri, b.ngaynhap, b.ghichu "
                + " from ERP_HH_TT a"
                + " left join ERP_HANGHOA d on (d.id_hh = a.id_hh)"
                + " left join ERP_KHACHHANG_SODO b on (a.id_thuoctinh = b.id_thuoctinh and a.id_hh = b.id_hh and b.id_khachhang = " + this._id_khachhang + ")"
                + " left join ERP_HH_THUOCTINH c on (c.id_thuoctinh = a.id_thuoctinh)"
                + " where a.sudung = 1 "
                + " and a.id_hh in (select id_monan from ERP_CHITIET_HOADON where id_hoadon = " + hd + ")";
                ds = csdl.SelecSQL(sql);

                return ds;
            }
            catch
            {
                return null;
            }
        }

        public DataSet Get_List_Nhapsodo_Theo_Hanghoa(int hh)
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                string sql = "select a.id_thuoctinh, a.id_hh, d.tenhh, c.tenthuoctinh, b.giatri, b.ngaynhap, b.ghichu "
                + " from ERP_HH_TT a"
                + " left join ERP_HANGHOA d on (d.id_hh = a.id_hh)"
                + " left join ERP_KHACHHANG_SODO b on (a.id_thuoctinh = b.id_thuoctinh and a.id_hh = b.id_hh and b.id_khachhang = " + this._id_khachhang + ")"
                + " left join ERP_HH_THUOCTINH c on (c.id_thuoctinh = a.id_thuoctinh)"
                + " where a.sudung = 1 "
                + " and a.id_hh = " + hh;
                ds = csdl.SelecSQL(sql);

                return ds;
            }
            catch
            {
                return null;
            }
        }

        public DataSet Get_List_Insodo(int id_hd)
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                string sql = "select row_number() over (order by a.id_hh) AS stt, a.id_thuoctinh, a.id_hh, d.tenhh, c.tenthuoctinh, b.giatri, b.ngaynhap, b.ghichu "
                + " from ERP_HH_TT a, ERP_HANGHOA d, ERP_KHACHHANG_SODO b, ERP_HH_THUOCTINH c, ERP_CHITIET_HOADON e"
                + " where a.sudung = 1 and b.giatri <> ' ' and d.id_hh = a.id_hh and a.id_thuoctinh = b.id_thuoctinh and a.id_hh = b.id_hh"
                + " and c.id_thuoctinh = a.id_thuoctinh and b.id_khachhang = " + this._id_khachhang
                + " and e.id_monan = a.id_hh and e.id_hoadon = " + id_hd;
                ds = csdl.SelecSQL(sql);

                return ds;
            }
            catch
            {
                return null;
            }
        }
    }
}